package cn.iocoder.yudao.module.wrzj.controller.app.greatman;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wrzj.controller.app.BaseWrzjController;
import cn.iocoder.yudao.module.wrzj.controller.app.greatman.vo.AppGreatmanRespVO;
import cn.iocoder.yudao.module.wrzj.controller.app.greatman.vo.GreatmanScore;
import cn.iocoder.yudao.module.wrzj.dal.dataobject.greatman.GreatmanDO;
import cn.iocoder.yudao.module.wrzj.dal.dataobject.wrzjuser.WrzjUserDO;
import cn.iocoder.yudao.module.wrzj.service.greatman.GreatmanService;
import cn.iocoder.yudao.module.wrzj.service.wrzjuser.WrzjUserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 伟人之家-用户")
@RestController
@RequestMapping("/wrzj/greatman")
@Validated
public class AppWrzjGreatmanController extends BaseWrzjController {

    @Resource
    private GreatmanService greatmanService;

    @Resource
    private WrzjUserService userService;

    @GetMapping("/get")
    @Operation(summary = "获得伟人详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppGreatmanRespVO> getUser(@RequestParam("id") Integer id) {
        GreatmanDO greatmanDO = greatmanService.getGreatman(id);
        return success(BeanUtils.toBean(greatmanDO, AppGreatmanRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得伟人列表")
//    @PreAuthorize("@ss.hasPermission('wrzj:greatman:query')")
    public CommonResult<List<AppGreatmanRespVO>> getCategoryList() {
        WrzjUserDO loginUser = userService.getUser(getLoginUid());
        String scores = loginUser.getScore();
        List<GreatmanScore> greatmanScoreList = JSONObject.parseArray(scores, GreatmanScore.class);
        List<GreatmanDO> greatmanList = greatmanService.getGreatmanList();
        List<AppGreatmanRespVO> appGreatmanRespVOList = BeanUtils.toBean(greatmanList, AppGreatmanRespVO.class);
        appGreatmanRespVOList.stream().forEach(greatman -> {
            greatmanScoreList.stream().forEach(greatmanScore -> {
                if (greatman.getId() == greatmanScore.getId().intValue()) {
                    greatman.setScore(greatmanScore.getScore());
                    greatman.setOwned(greatmanScore.isOwned());
                }
            });
        });
        return success(appGreatmanRespVOList);
    }

    @GetMapping("/ownedlist")
    @Operation(summary = "已召唤伟人列表")
//    @PreAuthorize("@ss.hasPermission('wrzj:greatman:query')")
    public CommonResult<List<AppGreatmanRespVO>> getOwnedList() {
        WrzjUserDO loginUser = userService.getUser(getLoginUid());
        String scores = loginUser.getScore();
        List<GreatmanScore> greatmanScoreList = JSONObject.parseArray(scores, GreatmanScore.class);
        List<GreatmanDO> greatmanList = greatmanService.getGreatmanList();
        List<AppGreatmanRespVO> appGreatmanRespVOList = BeanUtils.toBean(greatmanList, AppGreatmanRespVO.class);
        appGreatmanRespVOList = appGreatmanRespVOList.stream().filter(greatman -> {
            greatmanScoreList.stream().forEach(greatmanScore -> {
                if (greatman.getId() == greatmanScore.getId().intValue()) {
                    greatman.setScore(greatmanScore.getScore());
                    greatman.setOwned(greatmanScore.isOwned());
                }
            });
            return greatman.isOwned();
        }).collect(Collectors.toList());
        return success(appGreatmanRespVOList);
    }
}
